<#import "../../common/layout.ftl" as layout/>
<#import "../../common/dataTables.ftl" as dataTables/>
<#import "../../common/modern-forms.ftl" as mform/>
<#import "../../common/modal.ftl" as modal/>

<!DOCTYPE html>
<html>
<@layout.head title="数据表管理">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
</@layout.head>
<body>
<@layout.page>
<div class="main-box">

    <!-- 表格 -->
    <div class="content">
        <!-- 按钮 -->
        <div id="mUserBtnGroup" class="btn-group">
            <button type="button" class="btn btn-blue btn-sm"
                    onclick="javascript:refreshData();">
                <i class="fa fa-refresh"></i> 刷新
            </button>
        </div>
        <!-- 表格 -->
        <@dataTables.commonTable id="tabmUser"/>

    </div>
</div>
</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>

<script type="text/javascript">
    var columns =
            [
                {"title": "序号", "data": "No"},
                {"title": "数据表名", "data": "name"},
                {"title": "创建时间", "data": "crdate"},
                {"title": "最后操作时间", "data": "refdate"},
                {"title": "记录总数", "data": "rows"},
        {
            "title": "操作",
            "data": null,
            "sWidth": "17%",
            "className": "center btn-table",
            "defaultContent": "<button type=\"button\" id=\"asroles\" class=\"btn btn-link btn-table-icon\"  title=\"查看数据\"><i class='fa fa-eye' style='color:#336600'></i></button>"
        }
    ];

    var mUserTable = $('#tabmUser').winningTable({
        columns: columns,
        pageLength: 10,
        toolbar: "#mUserBtnGroup",
        /*select: {style: 'multi'},*/
        fixedHeader: false,
        searching: true,
        ordering: true,
        // "columnDefs": [{
        //     "targets": [0,1,2],
        //     "visible": false
        // }],
        buttons: [

        ],
        "rowCallback": function (row, data, index) {
            if (data.bState == true) {
                $('td:eq(2)', row).html('启用');
            } else if (data.bState == false){
                $('td:eq(2)', row).html('停用');
            }

            if (data.bState == false) {
                $('td', row).css('color', '#ff483f');
            }
        }
    });

    var uAr_columns =
            [
                {
                    "title": "active",
                    "data": null,
                    "className": "center",
                    "defaultContent": "<input type=\"checkbox\" id=\"selectAction\" class=\"editor-active\"/>"
                },
                {"title": "角色编码", "data": "cRoleID"},
                {"title": "角色名称", "data": "cRoleName"}
            ];
    var uAr_table = $('#tabUserasRole').winningTable({
        retrieve: true,
        columns: uAr_columns,
        paging: false,
        fixedHeader: false,
        buttons: [],
        select: {style: 'multi'},
        "columnDefs": [{
            "targets": [0, 1],
            "visible": false
        }],
        "createdRow": function (row, data, dataIndex) {
            if (data.active == '1') {
                uAr_table.dataTables.row(dataIndex).select();
            }
        }
    });

    $('#tabmUser').on("click", "#resetpassword", function () {
        var item = mUserTable.dataTables.row($(this).closest('tr')).data();
        swal({
            title: "确定重置密码",
            text: "是否确定将改用户密码重置为\"123456\"",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if(result.value)
            {
                $.ajax({
                    type: 'POST',
                    data: 'cUserCode=' + item.cUserCode,
                    url: '${request.contextPath}/usermanage/resetpassword',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            });
                        }
                        else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            }
        });
    });


    $('#tabmUser').on("click", "#asroles", function () {
        var item = mUserTable.dataTables.row($(this).closest('tr')).data();
        getRoles(item);
    });
    function refreshData() {
        mUserTable.queryData("tablemanage/queryAll");
    }
    refreshData();


    function getRoles(item) {
        top.addTabs({
            id: "tablemanage" +item.name,
            title: item.name,
            close: 'ture',
            url: "${request.contextPath}/tablemanage/table?name=" + item.name
        })

    }

</script>
</@layout.foot>
</body>
</html>